/*
 * @Author: Leo.wang wanglizhigs@163.com
 * @Date: 2023-11-09 16:34:03
 * @LastEditors: Leo.wang wanglizhigs@163.com
 * @LastEditTime: 2023-11-13 16:55:38
 * @FilePath: /three-react/src/pages/CurveGeometry/Line1.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { useEffect, useRef } from "react"
import createRender from '@/createRender';
import * as THREE from 'three'


const state = {}
export default function Page() {

  const container = useRef(null);
  useEffect(() => {
    if (!state.isMounted) {
      createRender(container)(render);
      state.isMounted = true;
    }
  }, [])



  function render(scene) {

    const geometry = new THREE.BufferGeometry()
    const curve = new THREE.CatmullRomCurve3([
      new THREE.Vector3(-50, 20, 90),
      new THREE.Vector3(-10, 40, 40),
      new THREE.Vector3(0, 0, 0),
      new THREE.Vector3(60, -60, 0),
      new THREE.Vector3(70, 0, 80)
    ])
    geometry.setFromPoints(curve.getPoints(50));

    const red = new THREE.Color(0xff0000)
    const green= new THREE.Color(0x00ff00)
    
    const count = geometry.attributes.position.count;
    const colors = []
    for (let index = 0; index < count; index++) {
      const percent = index / count;
      const color = red.clone().lerp(green, percent);
      // colors.push(percent, 0, 0)
      colors.push(color.r, color.g, color.b)
    }

  geometry.attributes.color = new THREE.BufferAttribute(new Float32Array(colors), 3)



    const material = new THREE.LineBasicMaterial({
      // color: 0x00ffff,
      vertexColors: true
    });
    const mesh = new THREE.Line(geometry, material);


    scene.add(mesh);
  }

  return (
    <div id="container" className="full container" ref={container}></div>
  )
}